package com.libinyn.base.pms.controller.sys;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.libinyn.base.pms.common.annotation.SysLog;
import com.libinyn.base.pms.common.utils.Result;
import com.libinyn.base.pms.common.validator.ValidatorUtils;
import com.libinyn.base.pms.controller.AbstractController;
import com.libinyn.base.pms.entity.sys.SysConfigEntity;
import com.libinyn.base.pms.serivce.sys.SysConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;


/**
 * 系统参数信息
 * @fileName: SysConfigController.java
 * @author: bin.li01
 * @date: 2017/12/13 22:31 
 * @version: v1.0.0
 */
@Controller
@RequestMapping("/sys/config")
public class SysConfigController extends AbstractController {
	@Resource
	private SysConfigService sysConfigService;
	
	/**
	 * 所有配置列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:config:list")
	public Result list(SysConfigEntity sysConfigEntity, PageInfo<SysConfigEntity> pageInfo){


		//如果不是超级管理员，则只查询自己创建的角色列表
		List<SysConfigEntity> sysConfigEntities;

		try {
			PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
			sysConfigEntities = sysConfigService.select(sysConfigEntity);
			pageInfo = new PageInfo<SysConfigEntity>(sysConfigEntities);

		} catch (Exception e) {
			logger.error("查询数据失败！",e);
			return Result.error("查询数据失败！");
		}

		return Result.ok().put("page", pageInfo);

	}
	
	
	/**
	 * 配置信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:config:info")
	public Result info(@PathVariable("id") Long id){
		SysConfigEntity config;
		try {
			config = sysConfigService.selectByPrimaryKey(id);
		} catch (Exception e) {
			return Result.error("查询数据失败");
		}

		return Result.ok().put("config", config);
	}
	
	/**
	 * 保存配置
	 */
	@SysLog("保存配置")
	@RequestMapping("/save")
	@RequiresPermissions("sys:config:save")
	public Result save(@RequestBody SysConfigEntity config){
		ValidatorUtils.validateEntity(config);

		sysConfigService.save(config);
		
		return Result.ok();
	}
	
	/**
	 * 修改配置
	 */
	@SysLog("修改配置")
	@RequestMapping("/update")
	@RequiresPermissions("sys:config:update")
	public Result update(@RequestBody SysConfigEntity config){
		ValidatorUtils.validateEntity(config);
		
		sysConfigService.update(config);
		
		return Result.ok();
	}
	
	/**
	 * 删除配置
	 */
	@SysLog("删除配置")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:config:delete")
	public Result delete(@RequestBody Long[] ids){
		try {
			sysConfigService.deleteBatch(ids);
		} catch (Exception e) {
			return Result.error("删除数据失败");
		}

		return Result.ok();
	}

}
